package export.generator;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import elements.Deal;
import elements.Shop;
import export.db.Loader;

public class Generator {
	private Loader loader;
	
	public Document getString(List<Deal> deals) {
		Document document = DocumentHelper.createDocument();
		Element dealsE = document.addElement("deals");
		for (Deal deal: deals) {
			Element dealE = dealsE.addElement("deal");
			
			Element websiteIdE = dealE.addElement("websiteId");
			websiteIdE.setText(Integer.toString(deal.getWebsiteId()));
			
			Element oidE = dealE.addElement("oid");
			oidE.setText(deal.getOid() == null?"":deal.getOid());
			
			Element titleE = dealE.addElement("title");
			titleE.setText(deal.getTitle() == null?"":deal.getTitle());
			
			Element dealUrlE = dealE.addElement("dealUrl");
			dealUrlE.setText(deal.getDealUrl() == null?"":deal.getDealUrl());
			
			Element sImageUrlE = dealE.addElement("sImageUrl");
			sImageUrlE.setText(deal.getSImageUrl() == null?"":deal.getSImageUrl());
			
			Element mImageUrlE = dealE.addElement("mImageUrl");
			mImageUrlE.setText(deal.getMImageUrl() == null?"":deal.getMImageUrl());
			
			Element lImageUrlE = dealE.addElement("lImageUrl");
			lImageUrlE.setText(deal.getLImageUrl() == null?"":deal.getLImageUrl());
			
			Element cityNameE = dealE.addElement("cityName");
			cityNameE.setText(deal.getCityName() == null?"":deal.getCityName());
			
			Element categoryE = dealE.addElement("category");
			categoryE.setText(deal.getCategory() == null?"":deal.getCategory());
			
			Element tipsE = dealE.addElement("tips");
			tipsE.setText(deal.getTips() == null?"":deal.getTips());
			
			Element priceE = dealE.addElement("price");
			priceE.setText(Double.toString(deal.getPrice()));
			
			Element dealValueE = dealE.addElement("dealValue");
			dealValueE.setText(Double.toString(deal.getDealValue()));
			
			Element discountPercentE = dealE.addElement("discountPercent");
			discountPercentE.setText(Double.toString(deal.getDiscountPercent()));
			
			Element startTimeE = dealE.addElement("startTime");
			startTimeE.setText(Long.toString(deal.getStartTime()));
			
			Element endTimeE = dealE.addElement("endTime");
			endTimeE.setText(Long.toString(deal.getEndTime()));
			
			Element shopsE = dealE.addElement("shops");
			for (Shop shop: deal.getShops()) {
				Element shopE = shopsE.addElement("shop");
				
				Element shopUrlE = shopE.addElement("shopUrl");
				shopUrlE.setText(shop.getShopUrl() == null?"":shop.getShopUrl());
				
				Element shopPhoneE = shopE.addElement("shopPhone");
				shopPhoneE.setText(shop.getShopPhone() == null?"":shop.getShopPhone());
				
				Element shopAddressE = shopE.addElement("shopAddress");
				shopAddressE.setText(shop.getShopAddress() == null?"":shop.getShopAddress());
				
				Element shopAreaE = shopE.addElement("shopArea");
				shopAreaE.setText(shop.getShopArea() == null?"":shop.getShopArea());
				
				Element longtitudeE = shopE.addElement("longtitude");
				longtitudeE.setText(Double.toString(shop.getLongtitude()));
				
				Element latitudeE = shopE.addElement("latitude");
				latitudeE.setText(Double.toString(shop.getLatitude()));
			}
			
		}
		return document;
	}
}
